package com.tozed.android.launcher.simpleui;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.tozed.android.launcher.LauncherApplication;
import com.tozed.android.launcher.R;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;

public class HiddenIconsManager
{
	private static final String FAVORITES_NAMESPACE = "http://schemas.android.com/apk/res/com.tozed.android.launcher";
	private static final String FILE_PATH = "/data/cust/xml/tz_launcher_hide_icons_config.xml";
	private static final String TAG = "HiddenIconsManager";
	private static final String TAG_FAVORITES = "favorites";
	private static HiddenIconsManager sInstance;
	private List<ComponentName> mHiddenIcons = new ArrayList<ComponentName>();

	private HiddenIconsManager()
	{
		loadHiddenIconsFromXml();
	}

	public static void destoryInstance()
	{
		if (sInstance != null){
			sInstance.getHiddenIcons().clear();
			sInstance = null;
		}
	}

	public static HiddenIconsManager getInstance()
	{
		if (sInstance == null)
			sInstance = new HiddenIconsManager();
		return sInstance;
	}
	
	private void loadHiddenIcons(XmlPullParser xmlPullParser)
	{
		AttributeSet attrs = Xml.asAttributeSet(xmlPullParser);
		try
		{
			// XmlUtils.beginDocument(paramXmlPullParser, "favorites");
			beginDocument(xmlPullParser, TAG_FAVORITES);
			int depth = xmlPullParser.getDepth();
			int type;

			while (((type = xmlPullParser.next()) != XmlPullParser.END_TAG || xmlPullParser
					.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT)
			{
				if (type != XmlPullParser.START_TAG){
					continue;
				}

				String strPkgName = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.tozed.android.launcher","packageName");
				String strClsName = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.tozed.android.launcher","className");
				if (!TextUtils.isEmpty(strPkgName) && !TextUtils.isEmpty(strClsName)){
					ComponentName componentName = new ComponentName(strPkgName, strClsName);
					//Log.d(TAG, "loadHiddenIcons, mHiddenIcons.add app: " + componentName);
					mHiddenIcons.add(componentName);
				}
			}
		} catch (XmlPullParserException xmlException){
			xmlException.printStackTrace();
			return;
		} catch (IOException e){
			e.printStackTrace();
		}
	}
	
	private void loadHiddenIconsFromXml()
	{
		Context context = LauncherApplication.getActiveInstance().getApplicationContext();
		/*
		 * read xml file from asset
		AssetManager manager = context.getAssets();
		InputStream inputStream = manager.open(filename);
	    XmlPullParser xmlPullParser = Xml.newPullParser();
	    xmlPullParser.setInput(inputStream, "utf-8");
	    */
	    
		// read xml from resource
		XmlResourceParser parser = context.getResources().getXml(R.xml.hidden_apps_simple);
		loadHiddenIcons(parser);
	}
	
	public void addHiddenIcon(ComponentName componentName){
		mHiddenIcons.add(componentName);
	}

	public List<ComponentName> getHiddenIcons(){
		return mHiddenIcons;
	}

	public boolean isHiddenIcon(ComponentName componentName){
		return mHiddenIcons.contains(componentName);
	}

	public boolean isHiddenIcon(Intent intent)
	{
		ComponentName componentName = intent.getComponent();
		if (componentName == null)
			return false;
		
		return isHiddenIcon(componentName);
	}
	
    private static final void beginDocument(XmlPullParser parser, String firstElementName)
            throws XmlPullParserException, IOException {
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG
                && type != XmlPullParser.END_DOCUMENT) {
            ;
        }

        if (type != XmlPullParser.START_TAG) {
            throw new XmlPullParserException("No start tag found");
        }

        if (!parser.getName().equals(firstElementName)) {
            throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
                    ", expected " + firstElementName);
        }
    }
}
